<?php

namespace backend\modules\pooai\controllers;

use backend\modules\base\controllers\BaseController;
use backend\modules\pooai\models\ZpooaiArtical;
use backend\modules\pooai\models\ZpooaiGallImages;
use Yii;
use backend\modules\pooai\models\ZpooaiPages;
use backend\modules\pooai\models\search\ZpooaiPagesSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * ZpooaipagesController implements the CRUD actions for ZpooaiPages model.
 */
class ZpooaipagesController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all ZpooaiPages models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ZpooaiPagesSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->getBodyParams());

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single ZpooaiPages model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new ZpooaiPages model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new ZpooaiPages();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['update', 'id' => $model->pooai_pages_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * 处理三个数据模型
     * Updates an existing ZpooaiPages model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        //前端数据
        $postData = Yii::$app->request->post();
        $model = $this->findModel($id);

        if ($model->load($postData) && $model->save()) {
            //提取文章提交前端数据
            $postArtical = $postData['ZpooaiArtical'];
            //判断是否添加还是修改
            if($postArtical['zpooai_artical_id']==null){
                $ZpooaiArtical = new ZpooaiArtical();
            } else {
                $ZpooaiArtical = ZpooaiArtical::findOne($id);
            }

            //页面文章模型赋值，标题内容
            $ZpooaiArtical->title = $postArtical['title'];
            $ZpooaiArtical->content = $postArtical['content'];
            $ZpooaiArtical->zpooai_pages_id = $id;
            if($ZpooaiArtical->save()){
                return $this->redirect(['update', 'id' => $model->pooai_pages_id]);
            }
            print_r($ZpooaiArtical->errors);exit;
        } else {

            //页面处理管理页面显示数据
            $ZpooaiGallImages = ZpooaiGallImages::find()->where(['zpooai_pages_id'=>$id])->orderBy(['sort_number'=>SORT_ASC])->asArray()->all();
            $ZpooaiArtical = ZpooaiArtical::find()->where(['zpooai_pages_id'=>$id])->one();
            if($ZpooaiArtical == null) {
                $ZpooaiArtical = new ZpooaiArtical();
            }

            return $this->render('update', [
                'model' => $model,
                'ZpooaiGallImages'=>$ZpooaiGallImages,
                'ZpooaiArtical'=>$ZpooaiArtical
            ]);
        }
    }

    /**
     * Deletes an existing ZpooaiPages model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the ZpooaiPages model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return ZpooaiPages the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ZpooaiPages::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    /**
     * 页面图片上传
     */
    public function actionUpload()
    {
        if($_FILES==null){
            echo  json_encode([]);exit;
        }
        $postData = Yii::$app->request->post();
        $baseUrl = Yii::$app->basePath;
        $baseImages = $baseUrl.'/web/images/pooai/';
        $sourceImg = $_FILES['ZpooaiPages']['tmp_name']['images'][0];
        $sourceName = $_FILES['ZpooaiPages']['name']['images'][0];
        $objPathFile = iconv("UTF-8","gb2312", $baseImages.$sourceName);

        if(move_uploaded_file($sourceImg,$objPathFile)){
            $zpooai_image  = 'web/images/pooai/'.$sourceName;
            $zpooai_pages_id = $postData['pooai_pages_id'];
            $ZpooaiGallImages = new ZpooaiGallImages();
            $ZpooaiGallImages->zpooai_image = $zpooai_image;
            $ZpooaiGallImages->zpooai_pages_id = $zpooai_pages_id;
            if($ZpooaiGallImages->save()) {
                echo  json_encode([]);exit;
            }
            echo json_encode("Failed2!");
        }else{
            echo json_encode("Failed!");
        }
    }
}
